package com.kusen.village.raise.controller;

import com.kusen.village.raise.entity.SysLoginForm;
import com.kusen.village.raise.entity.SysUserEntity;
import com.kusen.village.raise.entity.User;
import com.kusen.village.raise.service.UserService;
import com.kusen.village.raise.util.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * 登录控制器
 *
 * @author Ghl
 */
@Slf4j
@RestController
public class LoginController {
    @Autowired
    private UserService userService;

    /**
     * 登录
     */
    @PostMapping("/sys/login")
    public Map<String, Object> login(SysLoginForm form) throws IOException {
        Map<String, Object> r = new HashMap<>();
        if (form.getPassword() == null || form.getUsername() == null) {
            return R.error().put("code", 50001);
        }
        log.info("用户登入" + form);
        User user= userService.getUserByUserNameAndPassword(form);
        if(user!=null){
            form.setToken("admin-token");
            form.setAvatar("https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");
            form.setName("Super Admin");
            form.setIntroduction("I am a super administrator");
            r.put("data", form);
            log.info("用户登入成功"+user);
           return R.ok(r).put("code",20000);
        }else{
            r.put("data","用户名或者密码错误");
            return R.ok(r).put("code",50002);
        }


    }

    /**
     * 获取用户信息
     *
     * @param token
     * @return
     */
    @GetMapping("/user/info")
    public Map<String, Object> getToken(String token) {
        SysLoginForm form = new SysLoginForm();
        System.out.println(token);
        Map<String, Object> r = new HashMap<>();
        form.setToken("admin-token");
        form.setAvatar("https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");
        form.setName("Super Admin");
        form.setIntroduction("I am a super administrator");
        r.put("data", form);
        return R.ok(r).put("code", 20000);
    }

    @PostMapping("/user/logout")
    public Map<String, Object> logOut() {
        System.out.println("退出登入返回首页");
        return R.ok().put("code", 20000);
    }


}
